from django.shortcuts import render, redirect

from app01 import models
from app01.utils.pagination import Pagination
from app01.utils.form import AdminEditModelForm, AdminModelForm, AdminResetModelForm
from app01.utils.encrypt import md5


def admin_list(request):
    """ 管理员列表 """
    # cookie验证
    # info=request.session.get('info')
    # print(info)

    # 搜索
    search_data = request.GET.get('q', "")
    data_dict = {}
    if search_data:
        data_dict["username__contains"] = search_data
    queryset = models.Admin.objects.filter(**data_dict).order_by("-id")

    # 分页
    page_object = Pagination(request, queryset)
    context = {
        'queryset': page_object.page_queryset,
        'page_string': page_object.html(),
        'search_data': search_data,
    }
    return render(request, 'admin_list.html', context)


def admin_add(request):
    """ 添加管理员 """
    title = "新建管理员"

    if request.method == "GET":
        form = AdminModelForm()
        return render(request, "change.html", {"title": title, "form": form})
    form = AdminModelForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect("/admin/list/")
    return render(request, "change.html", {"title": title, "form": form})


def admin_edit(request, nid):
    """ 编辑管理员 """
    title = "编辑管理员"

    # 检验id是否存在,存在-对象/不存在-None
    row_object = models.Admin.objects.filter(id=nid).first()
    if not row_object:
        msg_error = "数据不存在！"
        return render(request, 'error.html', {"msg_error": msg_error})
    if request.method == "GET":
        form = AdminEditModelForm(instance=row_object)
        return render(request, 'change.html', {"form": form, "title": title})
    form = AdminEditModelForm(data=request.POST, instance=row_object)
    if form.is_valid():
        form.save()
        return redirect('/admin/list/')
    return redirect(request, 'change.html', {"form": form, "title": title})


def admin_delete(request, nid):
    """ 删除管理员 """
    row_object = models.Admin.objects.filter(id=nid).first()
    if not row_object:
        msg_error = "数据不存在！"
        return render(request, 'error.html', {"msg_error": msg_error})
    models.Admin.objects.filter(id=nid).delete()
    return redirect('/admin/list/')


# def admin_reset(request, nid):
#     """ 重置初始密码：123456 """
#     reset_password="123456"
#     row_object = models.Admin.objects.filter(id=nid).first()
#     if not row_object:
#         msg_error = "数据不存在！"
#         return render(request, 'error.html', {"msg_error": msg_error})
#     models.Admin.objects.filter(id=nid).update(password=md5(reset_password))
#     return redirect('/admin/list/')

def admin_reset(request, nid):
    """ 重置密码 """
    row_object = models.Admin.objects.filter(id=nid).first()
    if not row_object:
        msg_error = "数据不存在！"
        return render(request, 'error.html', {"msg_error": msg_error})

    title = "重置密码 - {}".format(row_object.username)
    if request.method=="GET":
        form = AdminResetModelForm()
        return render(request, 'change.html', {"form": form, "title": title})

    form=AdminResetModelForm(data=request.POST,instance=row_object)
    if form.is_valid():
        form.save()
        return redirect('/admin/list/')
    return render(request, 'change.html', {"form": form, "title": title})


